﻿using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Linq;
using ClientLauncher.Src.WPFUtils;
using Game500.ClientServerCommon.Src;

namespace ClientLauncher.Src.ViewModels
{
  public class MapSelectionViewModel : INotifyPropertyChanged
  {
    private bool? myDialogResult = false;

    public MapSelectionViewModel(string userName, BaseMapManager mapManager)
    {
      Caption = "User: " + userName;
      IEnumerable<MapInfoViewModel> maps = mapManager.GetAvailableMaps().Select(x => new MapInfoViewModel(x, mapManager.GetThumbnail(x.MapId).ToBitmapImage()));
      Items = new ObservableCollection<MapInfoViewModel>(maps);
    }

    public bool? DialogResult
    {
      get { return myDialogResult; }
      set
      {
        myDialogResult = value;
        RaisePropertyChanged("DialogResult");
      }
    }

    public string Caption { get; set; }

    public ObservableCollection<MapInfoViewModel> Items { get; private set; }

    public string Title { get; set; }

    public RelayCommand ConnectCommand
    {
      get { return new RelayCommand(o => { DialogResult = true; }, o => !string.IsNullOrWhiteSpace(Title) && SelectedItem != null); }
    }

    public MapInfoViewModel SelectedItem { get; set; }

    #region INotifyPropertyChanged Members

    public event PropertyChangedEventHandler PropertyChanged;

    #endregion

    private void RaisePropertyChanged(string pstrPropertyName)
    {
      if (PropertyChanged != null)
        PropertyChanged(this, new PropertyChangedEventArgs(pstrPropertyName));
    }
  }
}